﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Xml.Serialization;
using System.IO;
using NasuTek.Shion.GenuineAdvantage;
using System.Xml;
using System.Net;

namespace NasuTek.Shion
{
    public partial class setup : Form
    {
        public Configuration config;
        bool closing;

        public setup()
        {
            InitializeComponent();
        }

        private void setup_Load(object sender, EventArgs e)
        {
            GetConfig();
            if ((config.Name != null) && (config.Hash != null)) {
                if (Validator.ValidateHashes(Program.debug))
                {
                    MessageBox.Show("You already Activated your copy of Shion", "Shion Activation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    closing = true;
                    this.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {        
            if (button1.Text == "Close") { Application.Exit(); }
            if (panel2.Visible == true)
            {
                config.Name = textBox1.Text;
                config.Hash = textBox2.Text;
                SaveConfig();

                if (Validator.ValidateHashes(Program.debug))
                {
                    try { new WebClient().DownloadData("http://hinoki.odysseyt.com/shion6act/statistics.aspx?hash=" + config.Hash + "&bytes=" + HardwareKey.PackagedHardwareKey()); }
                    catch { }
                    closing = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Hash or Name is Invalid");
                    config.Name = "";
                    config.Hash = "";
                    SaveConfig();
                }
            }
            else
            {
                this.Close();
            }
        }

        public void GetConfig()
        {
            if (Program.debug == false)
            {
                if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6.xml")))
                {
                    XmlSerializer x = new XmlSerializer(typeof(Configuration));
                    FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6.xml"), FileMode.Open);
                    config = (Configuration)x.Deserialize(XmlReader.Create(fs));
                    fs.Close();
                }
                else { config = new Configuration(); }
            }
            else
            {
                if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6-debug.xml")))
                {
                    XmlSerializer x = new XmlSerializer(typeof(Configuration));
                    FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6-debug.xml"), FileMode.Open);
                    config = (Configuration)x.Deserialize(XmlReader.Create(fs));
                    fs.Close();
                }
                else { config = new Configuration(); }
            }
        }

        public void SaveConfig()
        {
            if (Program.debug == false)
            {
                XmlSerializer x = new XmlSerializer(typeof(Configuration));
                FileStream fs = File.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6.xml"));
                x.Serialize(XmlWriter.Create(fs), config);
                fs.Close();
            }
            else
            {
                XmlSerializer x = new XmlSerializer(typeof(Configuration));
                FileStream fs = File.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Shion6-debug.xml"));
                x.Serialize(XmlWriter.Create(fs), config);
                fs.Close();
            }
        }


        private void setup_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closing == false) { e.Cancel = true; }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new PrvPolicy().ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            closing = true;
            this.Close();
        }

    }
}
